#include "catalog.h"

//实现目录栈的插入和删除,打印功能仅供测试

int init_list(Link_list_t *catalog){
      catalog->head=NULL;
      catalog->tail=NULL;
      catalog->length=0;
      return 0;
  }

int add_catalog(Link_list_t *catalog,char *buf){
    if(strlen(buf)==0){
        return -1;
    }
    Node_t *newNode=(Node_t*)calloc(1,sizeof(Node_t));
    strcpy(newNode->data,buf);
    if(0==catalog->length){
        catalog->head=newNode;
        catalog->tail=newNode;
        catalog->length++;
    }
    else{
        catalog->tail->next_node=newNode;
        catalog->tail=newNode;
        catalog->length++;
    }
    return 0;
}

int delete_catalog(Link_list_t *catalog){
    if(catalog->length==0){
        return -1;
    }//空表检测

      if(1==catalog->length){
          free(catalog->head);
          catalog->tail=NULL;
          catalog->head=NULL;
          catalog->length--;
      }//单节点删除

      else{
      Node_t *temp=catalog->head;
      while(temp->next_node!=catalog->tail){
          temp=temp->next_node;
      }
          free(catalog->tail);
          catalog->tail=temp;
          catalog->tail->next_node=NULL;
          catalog->length--;
      }
      return 0;
  }

int printf_Linklist(Link_list_t *catalog){                                                                                                                         
    if(catalog->head==NULL){
        printf("Linklist is NULL\n");
        return 0;
    }
    Node_t *temp=catalog->head;
    while(temp->next_node){
        printf("%s--->",temp->data);
        temp=temp->next_node;
    }
    printf("%s\n",temp->data);
    return 0;
}





